const request = require('./request')

const album = {
  // 专辑封面
  img: async (query) => {
    const albummid = query.id;

    if (!albummid) {
      return {
        result: 500,
        errMsg: 'id 不能为空',
      };
    }


    const pageInfo = await request(`https://y.qq.com/n/yqq/album/${albummid}.html`);
    let html = pageInfo;

    html = html.substring(html.indexOf("coverUrl"));
    let result = html.substring(11,html.indexOf("\"}"));

    while (result.indexOf("\\u002F") !== -1){
      result = result.replace('\\u002F','/');
    }
    return result;


  },

  // 专辑的歌曲信息
  songs: async (query) => {
    const albummid =query.id;
    const {raw} = query;
    if (!albummid) {
      return{
        result: 500,
        errMsg: 'albummid 不能为空',
      };
    }
    let jsondata = JSON.stringify({
      comm: {
        ct: 24,
        cv: 10000
      },
      albumSonglist: {
        method: "GetAlbumSongList",
        param: {
          albumMid: albummid,
          albumID: 0,
          begin: 0,
          num: 999,
          order: 2
        },
        module: "music.musichallAlbum.AlbumSongList"
      }
    });

    const result = await request(
      'https://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=5381&format=json&inCharset=utf8&outCharset=utf-8&data='+jsondata)

    if (Number(raw)) {
      return result;
    }

    const resData = {
      result: 100,
      data: {
        list: result.albumSonglist.data.songList.map((item) => item.songInfo),
        total: result.albumSonglist.data.totalNum,
        albummid: result.albumSonglist.data.albumMid,
      }
    };

    return resData;
  }
};

module.exports = album

